<?php
namespace Home\Controller;
class UserOrderController extends CommonController{

	public function _initialize(){
		parent::_initialize();
		$this->user_id();
		$this->add_step('订单中心', U('index'));
	}
	// 订单列表
	public function index(){
		// $this->pagesize = 2;
		// D('Order')->pagesize = $this->pagesize;
		$type = $this->_get('type');
		switch ($type) {
			case 'all':
				$this->add_step('全部订单');
				break;
			// 待支付
			case 'wait_pay':
				$where['status'] = 'wait_pay';
				$this->add_step('待支付订单');
				break;
			// 待服务
			case 'wait_service':
				$where['status'] = array('in', 'doing-service,doing-lawyer,doing-audit');
				$this->add_step('待服务订单');
				break;
			// 待评价
			case 'wait_comment':
				$where['status']       = 'finish';
				$where['is_commented'] = 0;
				$this->add_step('待评价订单');
				break;
			
			default:
				
				break;
		}
		$this->assign('type', $type);
		// 订单号
		$order_sn = $this->_get('order_sn');
		if($order_sn){
			$where['order_sn'] = $order_sn;
		}
		$this->assign('order_sn', $order_sn);

		// 开始时间
		$start_date = $this->_get('start_date');
		if($start_date){
			$where['create_time'][] = array('egt',strtotime($start_date));
		}
		$this->assign('start_date', $start_date);


		// 结束时间
		$end_date = $this->_get('end_date');
		if($end_date){
			$where['create_time'][] = array('elt',strtotime($end_date . '23:59:59'));
		}
		$this->assign('end_date', $end_date);

		$where['user_id'] = $this->user_id;
		$list = D('Order')->get_list($where, '', $this->_get('p', '', 1));
		//处理AJAX分页
		$this->page($list, D('Order')->count);

		$this->assign('list', $list);
		$this->display();
	}
	// 订单详情
	public function detail(){
		$this->add_step('订单详情');
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info = D('Order')->info($order_sn);

		$this->assign('info', $info);
		$this->display();
	}
	// 评价
	public function win_comment(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info = D('Order')->info($order_sn);
		if(IS_POST){
			$label_id = implode(',', $this->_post('label_id'));
			$content  = $this->_post('content', '内容不能为空！');
			$finish_quality_star   = $this->_post('finish_quality_star');
			$working_speed_star    = $this->_post('working_speed_star');
			$service_attitude_star = $this->_post('service_attitude_star');
			D('Comment')->submit(
				'user_comment',
				$this->user_id,
				$order_sn,
				$content,
				'',
				$info['agreement_id'],
				$info['lawyer_user_id'],
				$finish_quality_star,
				$working_speed_star,
				$service_attitude_star,
				$label_id
			);

			$this->success('评价成功！', U('index', array('type' => 'wait_comment')));
		}

		$this->assign('info', $info);
		$this->display();
	}
	// 查看评价
	public function view_comment(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info = D('Order')->info($order_sn);

		// var_dump($info);
		$this->assign('info', $info);
		$this->display();
	}
	// 取消订单
	public function cancel(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		
		if(D('Order')->cancel($order_sn)){
			$this->success('取消订单成功！', U('detail', array('order_sn' => $order_sn)));
		}else{
			$this->error(D('Order')->tips_info);
		}
	}
	// 下载合同
	public function download(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$file_id  = $this->_get('file_id', '对不起，您的操作有误！');
		$info = D('Order')->info($order_sn);
		if($info['user_id'] != $this->user_id){
			$this->error('对不起，您的操作有误！');
		}
		if(!in_array($file_id, explode(',', $info['finish_file_ids']))){
			$this->error('对不起，您的操作有误！');
		}
		D('File')->download_file($file_id, $info['title']);
	}
}